/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86186
 * Date: 2023-08-28
 * Time: 21:19
 */
public class Test {
    //leetcode 判断字符是否唯一
    public boolean isUnique(String astr) {
        int tmp = 0;
        for(int i = 0; i < astr.length(); i++) {
            char ch = astr.charAt(i);
            if(((tmp >> (ch - 'a')) & 1) == 1) return false;
            else tmp = (tmp | (1 << (ch - 'a')));
        }

        return true;
    }

    //leetcode 268 丢失的数字
    public int missingNumber(int[] nums) {
        int tmp = 0;
        for(int i = 0; i < nums.length; i++) {
            tmp ^= nums[i];
        }

        for(int i = 0; i <= nums.length; i++) {
            tmp ^= i;
        }

        return tmp;
    }

    //leetcode 371 两整数之和
    public int getSum(int a, int b) {
        while(b != 0) {
            int x = a ^ b;
            int y = a & b;
            a = x;
            b = y << 1;
        }

        return a;
    }

    //leetcode 137 只出现一次的数字II
    public int singleNumber(int[] nums) {
        int ret = 0;
        for(int i = 0; i < 32; i++) {
            int tmp = 0;
            for(int j = 0; j < nums.length; j++) {
                tmp += ((nums[j] >> i) & 1);
            }
            tmp %= 3;
            ret |= (tmp << i);
        }

        return ret;
    }

    //leetcode 消失的两个数字
    public int[] missingTwo(int[] nums) {
        int tmp = 0;
        for(int n : nums) tmp ^= n;
        for(int i = 1; i <= nums.length + 2; i++) tmp ^= i;
        int flg = 0;
        for(int i = 0; i < 32; i++) {
            if(((tmp >> i) & 1) == 1) {
                flg = i;
                break;
            }
        }
        int[] ret = new int[2];
        for(int n : nums) {
            if(((n >> flg) & 1) == 1) ret[0] ^= n;
            else ret[1] ^= n;
        }
        for(int i = 1; i <= nums.length + 2; i++) {
            if(((i >> flg) & 1) == 1) ret[0] ^= i;
            else ret[1] ^= i;
        }

        return ret;
    }

    
}
